home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Metafiles / MetafileConvert / MetafileConvert.cs next >
Encoding:
Text File  |  2001-01-15  |  2.1 KB  |  65 lines

  1. //----------------------------------------------
  2. // MetafileConvert.cs ⌐ 2001 by Charles Petzold
  3. //----------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. using System.IO;              // For Path class
  8. using System.Windows.Forms;
  9.  
  10. class MetafileConvert: MetafileViewer
  11. {
  12.      public new static void Main()
  13.      {
  14.           Application.Run(new MetafileConvert());
  15.      }
  16.      public MetafileConvert()
  17.      {
  18.           Text = strProgName = "Metafile Convert";
  19.      }
  20.      protected override void MenuFileSaveAsOnClick(object obj, EventArgs ea)
  21.      {
  22.           SaveFileDialog dlg = new SaveFileDialog();
  23.  
  24.           if (strFileName != null && strFileName.Length > 0)
  25.                dlg.InitialDirectory = Path.GetDirectoryName(strFileName);
  26.  
  27.           dlg.FileName = Path.GetFileNameWithoutExtension(strFileName);
  28.           dlg.AddExtension = true;
  29.           dlg.Filter = "Windows Bitmap (*.bmp)|*.bmp|" +
  30.                        "Graphics Interchange Format (*.gif)|*.gif|" +
  31.                        "JPEG File Interchange Format (*.jpg)|" +
  32.                               "*.jpg;*.jpeg;*.jfif|" +
  33.                        "Portable Network Graphics (*.png)|*.png|" +
  34.                        "Tagged Image File Format (*.tif)|*.tif;*.tiff";
  35.  
  36.           if (dlg.ShowDialog() == DialogResult.OK)
  37.           {
  38.                Bitmap bm = MetafileToBitmap(mf);
  39.  
  40.                try
  41.                {
  42.                     bm.Save(dlg.FileName);
  43.                }
  44.                catch (Exception exc)
  45.                {
  46.                     MessageBox.Show(exc.Message, Text);
  47.                }
  48.           }
  49.      }
  50.      Bitmap MetafileToBitmap(Metafile mf)
  51.      {
  52.           Graphics grfx = CreateGraphics();
  53.           int cx = (int) (grfx.DpiX * mf.Width  / mf.HorizontalResolution);
  54.           int cy = (int) (grfx.DpiY * mf.Height / mf.VerticalResolution);
  55.           Bitmap bm = new Bitmap(cx, cy, grfx);
  56.           grfx.Dispose();
  57.  
  58.           grfx = Graphics.FromImage(bm);
  59.           grfx.DrawImage(mf, 0, 0, cx, cy);
  60.           grfx.Dispose();
  61.  
  62.           return bm;
  63.      }
  64. }
  65.